<template>
    <div class="login"></div>
</template>

<script>
import { setToken, removeToken } from "@/utils/auth";
import { Dialog, Toast } from "vant";

import { getToken, getTestToken, getUserInfo } from "@/api/user";

export default {
    name: "Login",
    created() {
        const { id } = this.$route.query;
        if (id) return this.getTestLogin(id);
        if (this.getQueryVariable("code")) {
            this.getLogin(this.getQueryVariable("code"));
        } else {
            this.login();
        }
        // this.getTestLogin(9710);
    },
    methods: {
        login() {
            let appid = "wwdc0bc731d475dfb8";
            let agentid = "1000156";
            let redirect_uri = "https://wechatwork.ihg.com.cn:8087/ipah5/index.html";
            let url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${encodeURI(
                appid
            )}&redirect_uri=${encodeURIComponent(
                redirect_uri
            )}&response_type=code&scope=snsapi_privateinfo&state=STATE&agentid=${encodeURI(agentid)}#wechat_redirect`;
            window.location.href = url;
        },
        // 测试入口
        getTestLogin(id) {
            Toast.loading({
                forbidClick: true,
                duration: 0,
            });
            getTestToken({
                // type: 2, // 测试 角色
                id, // 测试传角色id
            })
                .then(async (res) => {
                    Toast.clear();
                    if (res.status_code != 200) {
                        removeToken();
                        Dialog.alert({
                            title: "提示",
                            message: res.message,
                        }).then(() => {});
                    } else {
                        const { token } = res.data;
                        setToken(token);
                        localStorage.setItem("userinfo", JSON.stringify(res.data));

                        if (res.data.type !== 4) {
                            const user = await getUserInfo();
                            if (user.data.hotels && user.data.hotels instanceof Array && user.data.hotels.length > 0) {
                                let hotels = user.data.hotels[0];
                                hotels.hotel_index = 0;
                                localStorage.setItem("hotels", JSON.stringify(hotels));
                            } else {
                                localStorage.removeItem("hotels");
                            }
                        }

                        if (res.data.type === 3) {
                            // window.location.replace('https://wechatwork.ihg.com.cn:8087/ipah5/#/formsInfo')
                            this.$router.replace({
                                path: "formsInfo",
                            });
                            return;
                        }
                        if (res.data.type === 2 || res.data.type === 4) {
                            // window.location.replace('https://wechatwork.ihg.com.cn:8087/ipah5/#/customer')
                            this.$router.replace({
                                path: "customer",
                            });
                            return;
                        }
                        // window.location.replace('https://wechatwork.ihg.com.cn:8087/ipah5/#/index')

                        this.$router.replace({
                            path: "index",
                        });
                    }
                })
                .catch((err) => {
                    Toast.clear();
                    console.log(err);
                });
        },
        getLogin(code) {
            Toast.loading({
                forbidClick: true,
                duration: 0,
            });
            getToken({
                code,
            })
                .then(async (res) => {
                    Toast.clear();
                    if (res.status_code != 200) {
                        removeToken();
                        Dialog.alert({
                            title: "提示",
                            message: res.message,
                        }).then(() => {});
                    } else {
                        const { token } = res.data;
                        setToken(token);
                        localStorage.setItem("userinfo", JSON.stringify(res.data));

                        const user = await getUserInfo();
                        if (user.data.hotels && user.data.hotels instanceof Array && user.data.hotels.length > 0) {
                            let hotels = user.data.hotels[0];
                            hotels.hotel_index = 0;
                            localStorage.setItem("hotels", JSON.stringify(hotels));
                        } else {
                            localStorage.removeItem("hotels");
                        }
                        if (res.data.type === 3) {
                            window.location.replace("https://wechatwork.ihg.com.cn:8087/ipah5/#/formsInfo");
                            // this.$router.replace({
                            // 	path: "formsInfo"
                            // });
                            return;
                        }
                        if (res.data.type === 2) {
                            window.location.replace("https://wechatwork.ihg.com.cn:8087/ipah5/#/customer");
                            // this.$router.replace({
                            // 	path: "customer"
                            // });
                            return;
                        }
                        window.location.replace("https://wechatwork.ihg.com.cn:8087/ipah5/#/index");

                        // this.$router.replace({
                        // 	path: "index"
                        // });
                    }
                })
                .catch((err) => {
                    Toast.clear();
                    console.log(err);
                });
        },
        getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) {
                    return pair[1];
                }
            }
            return false;
        },
    },
};
</script>
<style type="text/css" lang="scss" scoped></style>
